using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;

namespace DD.Data.SQL
{
    public static class ProductManager
    {
        public static DataSet GetProduct(int productId)
        {
            SqlConnection conn = DD.Data.SQL.DataManager.GetConnection();

            SqlCommand com = new SqlCommand("sp_GET_PRODUCT", conn);
            com.CommandType = CommandType.StoredProcedure;

            SqlParameter PID = new SqlParameter("@PRODUCT_ID", productId);
            PID.SqlDbType = System.Data.SqlDbType.Int;

            com.Parameters.Add(PID);

            SqlDataAdapter da = new SqlDataAdapter(com);

            DataSet returnDS = new DataSet();

            da.Fill(returnDS);
            return returnDS;

        }

        public static int AddProduct(int active, double cost, string name, int onsale, double price, double saleprice, DD.Products.ProductType type, string description)
        {
            SqlConnection conn = DD.Data.SQL.DataManager.GetConnection();

            SqlCommand com = new SqlCommand("gp_DD_PRODUCTS_Insert", conn);
            com.CommandType = CommandType.StoredProcedure;

            SqlParameter NewId = new SqlParameter("@ID", SqlDbType.Int);
            NewId.Direction = ParameterDirection.Output;
           
            SqlParameter PID = new SqlParameter("@ACTIVE", active);
            PID.SqlDbType = System.Data.SqlDbType.Int;

            SqlParameter PID1 = new SqlParameter("@COST", cost);
            PID1.SqlDbType = System.Data.SqlDbType.Decimal;

            SqlParameter PID2 = new SqlParameter("@NAME", name);
            PID2.SqlDbType = System.Data.SqlDbType.NVarChar;

            SqlParameter PID3 = new SqlParameter("@ONSALE", onsale);
            PID3.SqlDbType = System.Data.SqlDbType.Int;

            SqlParameter PID4 = new SqlParameter("@PRICE", price);
            PID4.SqlDbType = System.Data.SqlDbType.Decimal;

            SqlParameter PID5 = new SqlParameter("@SALEPRICE", saleprice);
            PID5.SqlDbType = System.Data.SqlDbType.Decimal;
            
            SqlParameter PID6 = new SqlParameter("@TYPE", type);
            PID6.SqlDbType = System.Data.SqlDbType.Int;

            SqlParameter PID7 = new SqlParameter("@DESCRIPTION", description);
            PID7.SqlDbType = System.Data.SqlDbType.NVarChar;

            
            
            com.Parameters.Add(PID);
            com.Parameters.Add(PID1);
            com.Parameters.Add(PID2);
            com.Parameters.Add(PID3);
            com.Parameters.Add(PID4);
            com.Parameters.Add(PID5);
            com.Parameters.Add(PID6);
            com.Parameters.Add(PID7);
            com.Parameters.Add(NewId);

            conn.Open();
            com.ExecuteNonQuery();
            conn.Close();

            return (int)NewId.Value;
        }

        public static void AddProductStyle(int pid, int sid, string did)
        {
            SqlConnection conn = DD.Data.SQL.DataManager.GetConnection();

            SqlCommand com = new SqlCommand("sp_DD_AddProductStyle", conn);
            com.CommandType = CommandType.StoredProcedure;

            SqlParameter PID = new SqlParameter("@PID", pid);
            PID.SqlDbType = SqlDbType.Int;
            com.Parameters.Add(PID);

            SqlParameter SID = new SqlParameter("@SID", sid);
            SID.SqlDbType = System.Data.SqlDbType.Int;
            com.Parameters.Add(SID);

            SqlParameter DID = new SqlParameter("@DID", did);
            DID.SqlDbType = System.Data.SqlDbType.NVarChar;
            com.Parameters.Add(DID);


            conn.Open();
            com.ExecuteNonQuery();
            conn.Close();
        }

        public static void AddProductCategory(int pid, int cid)
        {

            SqlConnection conn = DD.Data.SQL.DataManager.GetConnection();

            SqlCommand com = new SqlCommand("sp_DD_ADD_PRODUCT_CATEGORY", conn);
            com.CommandType = CommandType.StoredProcedure;

            SqlParameter PID = new SqlParameter("@PID", pid);
            PID.SqlDbType = SqlDbType.Int;
            com.Parameters.Add(PID);

            SqlParameter SID = new SqlParameter("@CID", cid);
            SID.SqlDbType = System.Data.SqlDbType.Int;
            com.Parameters.Add(SID);

            conn.Open();
            com.ExecuteNonQuery();
            conn.Close();

        }
    }
}
